package com.yjy.fuyao.stu.controller;


import com.yjy.fuyao.stu.service.CourseCollectService;
import com.yjy.fuyao.utils.result.R;
import com.yjy.fuyao.utils.result.ResponseEnum;
import com.yjy.fuyao.utils.util.JwtUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 课程收藏 前端控制器
 * </p>
 *
 * @author yjy
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/front/stu/collect")
public class CourseCollectController {
    @Resource
    private CourseCollectService courseCollectService;

    @PostMapping("/star/{courseId}")
    public R starCourse(@ApiParam(name = "courseId", value = "课程ID", required = true)
                            @PathVariable String courseId, HttpServletRequest request) {
        String userId = JwtUtils.getUserIdByJwtToken(request);
        if(userId == ""){
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
        }
        courseCollectService.addCollect(courseId, userId);
        return R.ok();
    }

    @PostMapping("/undoStar/{courseId}")
    public R undoStarCourse(@ApiParam(name = "courseId", value = "课程ID", required = true)
                        @PathVariable String courseId, HttpServletRequest request) {
        courseCollectService.deleteCollect(JwtUtils.getUserIdByJwtToken(request),courseId);
        return R.ok();
    }
}

